package editManager;

import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.Color;

import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JTable;

import java.awt.Dimension;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class InWorkPanel extends JPanel implements Serializable {
	private JButton btnBack, btnFindEdit, btnChangeToQuote;
	private JTable table;
	private DefaultTableModel tableModel;
	private JScrollPane scrollPane;
	private JLabel lastTimeSentLabel, editTotalNumberLabel;
	private File currentEditFile;
	private EditRun currentEditRun;
	private int editTotal;
	private Hashtable<Integer, Edit> editDB;

	
	/**
	 * Create the panel.
	 */

	public InWorkPanel() {
		editTotal = 0;
		setPreferredSize(new Dimension(600, 400));
		setBackground(new Color(152, 251, 152));
		setLayout(null);
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnBack.setBounds(478, 353, 89, 23);
		add(btnBack);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 425, 365);
		add(scrollPane);

		tableModel = new DefaultTableModel();
		tableModel.addColumn("Cut Number");
		tableModel.addColumn("Customer");
		tableModel.addColumn("Customer ID");
		
		table = new JTable(tableModel);
		table.getColumnModel().getColumn(1).setPreferredWidth(160);
		scrollPane.setViewportView(table);
		
		JLabel lblEditsLastSent = new JLabel("Edits Last Sent:");
		lblEditsLastSent.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblEditsLastSent.setBounds(471, 25, 75, 14);
		add(lblEditsLastSent);
		
		lastTimeSentLabel = new JLabel("Time t");
		lastTimeSentLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lastTimeSentLabel.setBounds(546, 25, 39, 14);
		add(lastTimeSentLabel);
		
		btnFindEdit = new JButton("Find Edit");
		btnFindEdit.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnFindEdit.setBounds(478, 50, 89, 23);
		add(btnFindEdit);
		
		btnChangeToQuote = new JButton("Change to Quote");
		btnChangeToQuote.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnChangeToQuote.setBounds(461, 84, 122, 23);
		add(btnChangeToQuote);
		
		JLabel lblTotalEdits = new JLabel("Total Edits:");
		lblTotalEdits.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblTotalEdits.setBounds(478, 12, 54, 14);
		add(lblTotalEdits);
		
		editTotalNumberLabel = new JLabel("#####");
		editTotalNumberLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		editTotalNumberLabel.setBounds(534, 12, 48, 14);
		add(editTotalNumberLabel);
		
		File currentEditFile = new File("Z:\\AAPRINT-EDIT");

	}

	public boolean checkForNew(){
		File checkEditFile = new File("Z:\\AAPRINT-EDIT");
		if(currentEditFile.lastModified() == checkEditFile.lastModified()){
			return true;
		}
		return false;
	}	
	
	public EditRun generateEditRun() {
		//currentEditFile = new File("Z:\\AAPRINT-EDIT");DEBUG
		currentEditFile = new File("C:\\Users\\jedwards\\Desktop\\Development\\jtest");
		try {
			currentEditRun = new EditRun(currentEditFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		currentEditRun.sendEmail();
		currentEditRun.createFile();
		updateTableData();		
		editTotal = tableModel.getRowCount();
		editTotalNumberLabel.setText(""+ editTotal);
		editTotalNumberLabel.repaint();
		
		return currentEditRun;
	}
	
	private void updateTableData(){
		for(Edit e : currentEditRun.getAllEdits()){
			tableModel.addRow(new Object[]{e.getCutNumber(), e.getCustomerName(), e.getCustomerNumber()});
			tableModel.fireTableDataChanged();
		}
				
	}
	
	public JButton getBtnBack() {
		return btnBack;
	}

	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	public JButton getBtnFindEdit() {
		return btnFindEdit;
	}

	public void setBtnFindEdit(JButton btnFindEdit) {
		this.btnFindEdit = btnFindEdit;
	}

	public JButton getBtnChangeToQuote() {
		return btnChangeToQuote;
	}

	public void setBtnChangeToQuote(JButton btnChangeToQuote) {
		this.btnChangeToQuote = btnChangeToQuote;
	}
	
	public void setScrollPane(JScrollPane scrollPane) {
		this.scrollPane = scrollPane;
	}

	public JLabel getLastTimeSentLabel() {
		return lastTimeSentLabel;
	}

	public void setLastTimeSentLabel(JLabel lastTimeSentLabel) {
		this.lastTimeSentLabel = lastTimeSentLabel;
	}

	public JLabel getEditTotalNumberLabel() {
		return editTotalNumberLabel;
	}

	public void setEditTotalNumberLabel(JLabel editTotalNumberLabel) {
		this.editTotalNumberLabel = editTotalNumberLabel;
	}
	
}
